﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LastoRevival.Service.Implementations.Data;
using LastoRevival.Service.Implementations.Data.Sql;
using LastoRevival.Service.Implementations.Settings;

namespace LastoRevival.Service.Implementations
{
	static class DependencyFactory
	{
		private static object singletonLock = new object();

		private static IDataProvider dataProvider;

		// the hit will be larger, but we'll do one big setup on first hit
		// the first hit will actually be from app start, but we'll make sure we do a check
		// in all the right places anyway

		public static void SetupDependencies()
		{
			lock (singletonLock)
			{
				// hard code sql, eventually we could support other providers
				var settings = ServiceSettings.GetInstance();
				dataProvider = SqlDataProvider.Create(settings.ConnectionString);
			}
		}


		public static IDataProvider GetDataProvider()
		{
			if (dataProvider == null)
			{
				lock (singletonLock)
				{
					if (dataProvider == null)
					{
						SetupDependencies();
					}
				}
			}

			return dataProvider;
		}

	}
}
